import echarts from "echarts";
import request from "@/utils/request";

let diagData = [];
let contactData = [];
let inputDiagnostic = [];
let medicalLoad = 0;

export default {

  //各区确诊人数分布-饼形图
  chartFunc1() {
    request({
      url: '/eim/diagnostic_info/getAddressAndNum',
      method: 'post'
    }).then(res => {
      let labelData = [];
      let chartData = res.data;
      for(let i=0; i<res.data.length; i++){
        chartData[i].name = this.getAddressById(chartData[i].name);
        labelData.push(chartData[i].name);
      }
      this.renderChart1(labelData, chartData);
    });
  },

  renderChart1(labelData, chartData){
    this.chart1 = echarts.init(document.getElementById('chart1-pie'));
    let option = {
      tooltip: {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
      },
      legend: {
        orient: 'horizontal',
        bottom: 'left',
        data: labelData,
        textStyle: {color: '#fff'}
      },

      series: [
        {
          name: '',
          type: 'pie',
          radius: '65%',
          center: ['50%', '45%'],
          // data: [{value: 2, name: '朝阳区'},{value: 3, name: '二道区'},{value: 1, name: '宽城区'},{value: 3, name: '绿园区'},{value: 5, name: '汽开区'}],
          data: chartData,
        }
      ]
    };
    this.chart1.setOption(option);
  },

  //近七日确诊及密接人数-柱状图
  chartFunc2() {
    request({
      url: '/eim/diagnostic_info/getDiagnosticNumAndDate',
      method: 'post',
      async:false
    }).then(res1 => {
      request({
        url: '/eim/contact_info/getCloseContactNumAndDate',
        method: 'post',
        async:false
      }).then(res2 => {
        //图表数据格式转换
        let data = [];
        let title = ['time', '确诊病例', '接触人员'];
        data.push(title);
        let days = this.getNearly7Day().reverse();
        for(let i=0; i<days.length; i++){
          let subDate = [days[i],0,0];
          for (let j=0; j<res1.data.length; j++){
            if(res1.data[j].date === days[i]){
              subDate[1] = res1.data[j].value;
            }
          }
          for (let k=0; k<res2.data.length; k++){
            if(res2.data[k].date === days[i]){
              subDate[2] = res2.data[k].value;
            }
          }
          data.push(subDate);
        }

        //全局数据获取
        for(let i=0; i<data.length-1; i++){
          let tempData = {};
          tempData.x = i;
          tempData.y = data[i+1][1];
          diagData.push(tempData);
          tempData.y = data[i+1][2];
          contactData.push(tempData);
        }

        this.renderChart2(data);
      });
    });
  },

  renderChart2(data){
    //表格初始化
    this.chart2 = echarts.init(document.getElementById('chart2-column'));
    //表格设置
    let option = {
      legend: {
        x: 'right',
        y: 'bottom',
        textStyle: {
          color: '#4ADEFE',
        }
      },
      grid: {
        x: 30,
        y: 50,
        x2: 10,
        y2: 45
      },
      tooltip: {},
      dataset: {
        source: data
        // [['time', '确诊病例', '接触人员'],
        // ['10日', 1, 23],
        // ['11日', 0, 11],
        // ['12日', 1, 24],
        // ['13日', 2, 51],
        // ['14日', 2, 52],
        // ['15日', 3, 59],
        // ['16日', 5, 72]]
      },
      xAxis: {
        type: 'category',
        axisLine: {
          lineStyle: {
            color: "#4ADEFE",
          },
        },
        axisTick: {
          alignWithLabel: true
        }
      },
      yAxis: {
        axisLine: {
          lineStyle: {
            color: "#4ADEFE",
          }
        },
        splitLine: false
      },
      series: [
        {
          type: 'bar',
          barMaxWidth: '18',
          itemStyle: {
            normal: {
              color: 'red',
              label: {
                color: 'white',
                show: true,
                position: 'top'
              }
            },
          },
        },
        {
          type: 'bar',
          barMaxWidth: '18',
          itemStyle: {
            normal: {
              color: 'yellow',
              label: {
                color: 'white',
                show: true,
                position: 'top'
              }
            },
          },
        }
      ]
    };
    this.chart2.setOption(option);
  },

  //近七日境外输入人员-折线图
  chartFunc3() {
    request({
      url: '/eim/citizen/getOuterCitizen',
      method: 'post'
    }).then(res1 => {
      request({
        url: '/eim/citizen/getOuterDiagnostic',
        method: 'post'
      }).then(res2 => {
        //获取近七日入境人员和境外输入病例，进行数据转换
        let label = this.getNearly7Day().reverse();
        let dataOutCitizen= [];
        let dataOutDiagnostic= [];
        for(let i=0; i<label.length; i++){
          dataOutCitizen[i] = 0;
          dataOutDiagnostic[i] = 0;
          for (let j=0; j<res1.data.length; j++){
            if(res1.data[j].date === label[i]){
              dataOutCitizen[i] = res1.data[j].value;
            }
          }
          for (let k=0; k<res2.data.length; k++){
            if(res2.data[k].date === label[i]){
              dataOutDiagnostic[i] = res2.data[k].value;
            }
          }
        }
        //获取全局数据
        for(let i=0; i<dataOutDiagnostic.length; i++){
          let tempData = {};
          tempData.x = i;
          tempData.y = dataOutDiagnostic[i];
          inputDiagnostic.push(tempData);
        }
        this.renderCharts3(label,dataOutCitizen, dataOutDiagnostic);
      });
    });
  },

  renderCharts3(label, dataOutCitizen, dataOutDiagnostic){
    this.chart3 = echarts.init(document.getElementById('chart3-line'));
    let option = {
      grid: {
        x: 35,
        y: 50,
        x2: 40,
        y2: 45
      },
      legend: {
        x: 'right',
        y: 'bottom',
        textStyle: {
          color: '#4ADEFE',
        },
        data: ['输入病例', '入境人数']
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        axisLine: {
          lineStyle: {
            color: '#4ADEFE'
          }
        },
        splitLine: {
          lineStyle: {
            color: '#4ADEFE'
          }
        },
        axisLabel: {
          color: "#4ADEFE"
        },
        data: label
        //['10日', '11日', '12日', '13日', '14日', '15日', '16日']
      },
      yAxis: {
        type: 'value',
        axisLine: {
          lineStyle: {
            color: '#4ADEFE'
          }
        },
        splitLine: false,
        axisLabel: {
          color: '#4ADEFE'
        },
      },
      series: [
        {
          name: '输入病例',
          type: 'line',
          stack: '总量',
          areaStyle: {normal: {}},
          lineStyle: {
            normal: {
              color: 'red',
              opacity: 2
            }
          },
          data: dataOutDiagnostic,
          // [2, 1, 2, 3, 5, 4, 6],
          itemStyle: {
            normal: {
              label: {
                color: 'white',
                show: true,
                position: 'top'
              }
            }
          }
        },
        {
          name: '入境人数',
          type: 'line',
          stack: '总量',
          data: dataOutCitizen,
          //[20, 12, 20, 34, 58, 54, 69],
          areaStyle: {normal: {}},
          lineStyle: {
            normal: {
              color: 'blue',
              opacity: 2
            }
          },
          itemStyle: {
            normal: {
              label: {
                color: 'white',
                show: true,
                position: 'top'
              }
            }
          }
        }
      ]
    };
    this.chart3.setOption(option);
  },

  //疫情分布地图-地图
  chartFunc5(cc) {
    request({
      url: '/eim/staypoint/list',
      method: 'get'
    }).then(res => {
      //数据准备
      //接触点经纬度
      let positionCood = {};
      //接触点等级
      let levels = [];
      //数据格式转换
      for(let i=0; i<res.rows.length; i++){
        let name = res.rows[i].address;
        let cood = [res.rows[i].longitude, res.rows[i].latitude];
        positionCood[name] = cood;

        let level = {};
        level.name = res.rows[i].address;
        level.value = res.rows[i].type;
        levels.push(level);
      }
      this.renderChart5(cc,positionCood,levels)
    });
  },

  renderChart5(cc,positionCood,levels){
    //基于准备好的dom，初始化echarts实例
    this.chart5 = echarts.init(document.getElementById('chart5-map'));
    //注册长春地图
    echarts.registerMap('changchun', cc, {});
    //标记点坐标
    let geoCoordMap = positionCood;
    // {'朝阳区': [125.288319, 43.733513],'二道区': [125.72429, 43.86501],'宽城区': [125.32635, 44.11182],'绿园区': [125.21582, 43.93045],'汽开区': [125.124097, 43.874493],};
    //地点对应数据
    let data = levels;
    // [{name: '朝阳区', value: 50},{name: '二道区', value: 14},{name: '宽城区', value: 17},{name: '绿园区', value: 23},{name: '汽开区', value: 98}];
    //标记点与对应数据格式转化，，转换后的格式：[{name: 'cityName', value: [lng, lat, val]}, {...}]
    let convertData = function (data) {
      let res = [];
      for (let i = 0; i < data.length; i++) {
        let geoCoord = geoCoordMap[data[i].name];
        if (geoCoord) {
          res.push({
            name: data[i].name,
            value: geoCoord.concat(data[i].value)
          });
        }
      }
      return res;
    };

    //地图配置项
    let option = {
      //图标标题
      title: {
        text: '',
        top: 20,
        subtext: '',
        x: 'center',
        textStyle: {color: '#ccc'}
      },
      //图例
      legend: {
        data: [''],
        orient: 'vertical',
        y: 'bottom',
        x: 'right',
        textStyle: {color: '#fff'}
      },
      //图表说明
      tooltip: {
        trigger: 'item',
        formatter: function (params) {
          return params.name;
        }
      },
      //根据数值显示地图颜色
      visualMap: {
        min: 0,
        max: 2,
        calculable: true,
        inRange: {
          color: ['skyblue', 'yellow', 'red']
        },
        textStyle: {
          color: '#fff'
        }
      },
      geo: {
        map: 'changchun',
        mapType: 'changchun',
        roam: true, //鼠标缩放和平移
        zoom: 2.5,
        center: [125.3245,43.886841],
        label: {
          normal: {},
          //鼠标移入后查看效果
          emphasis: {
            textStyle: {
              show: true,
              color: '#fff'
            }
          }
        },
        //各子区域显示配置
        itemStyle: {
          normal: {
            borderColor: 'rgba(147, 235, 248, 1)',
            borderWidth: 1,
            areaColor: {
              type: 'radial',
              x: 0.5,
              y: 0.5,
              r: 0.8,
              colorStops: [{
                offset: 0,
                color: 'rgba(175,238,238, 0)' // 0% 处的颜色
              }, {
                offset: 1,
                color: 'rgba(	47,79,79, .2)' // 100% 处的颜色
              }],
              globalCoord: false // 缺省为 false
            },
            shadowColor: 'rgba(128, 217, 248, 1)',
            shadowOffsetX: -2,
            shadowOffsetY: 2,
            shadowBlur: 10
          },
          emphasis: {
            areaColor: '#389BB7',
            borderWidth: 0
          }
        }
      },
      series: [
        {
          name: 'caseNum',
          type: 'scatter',
          coordinateSystem: 'geo',
          zoom: 2,
          symbolSize: 10,
          label: {
            normal: {
              show: false,
            },
            emphasis: {
              show: false
            }
          },
          itemStyle: {
            emphasis: {
              borderColor: '#fff',
              borderWidth: 1
            }
          },
          data: convertData(data),
        },
      ]
    }
    this.chart5.setOption(option);
  },

  //疫情历史数据-折线图
  chartFunc6() {
    request({
      url: '/eim/history/getEimDataHistory',
      method: 'post'
    }).then(res => {
      let label=[];
      let dataDiagnostic=[];
      let dataCloseContact=[];
      let dataDeath=[];
      for(let i=0; i<res.data.length; i++){
        label.push(res.data[i].modifyTime);
        dataDiagnostic.push(res.data[i].diagnosticNum);
        dataCloseContact.push(res.data[i].closeContactNum);
        dataDeath.push(res.data[i].deathNum);
      }
      this.renderChart6(label, dataDiagnostic, dataCloseContact, dataDeath);
    });
  },

  renderChart6(label, dataDiagnostic, dataCloseContact, dataDeath){
    this.chart6 = echarts.init(document.getElementById('chart6-line'));
    let option = {
      grid: {
        x: 30,
        y: 50,
        x2: 20,
        y2: 45
      },
      legend: {
        x: 'right',
        y: 'bottom',
        textStyle: {
          color: '#4ADEFE',
        },
        data: ['累计确诊病例', '累计死亡病例', '累计密接人数']
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        axisLine: {
          lineStyle: {
            color: '#4ADEFE'
          }
        },
        splitLine: {
          lineStyle: {
            color: '#4ADEFE'
          }
        },
        axisLabel: {
          color: "#4ADEFE"
        },
        data: label
        //['2020年5月', '2020年8月', '2020年11月', '2021年2月', '2021年5月', '2021年8月', '2021年11月', '2022年2月', '2022年5月', '2022年8月', '当前']
      },
      yAxis: {
        type: 'value',
        axisLine: {
          lineStyle: {
            color: '#4ADEFE'
          }
        },
        splitLine: {
          lineStyle: {
            type: 'dashed',
            color: '#4ADEFE'
          }
        },
        axisLabel: {
          color: '#4ADEFE'
        },
      },
      series: [
        {
          name: '累计确诊病例',
          type: 'line',
          //areaStyle: {normal: {}},
          data: dataDiagnostic,
          //[0, 5, 20, 30, 50, 400, 5000, 21000, 21500, 22500, 22800],
          lineStyle: {
            normal: {
              color: 'red',
              opacity: 1
            }
          },
          itemStyle: {
            normal: {
              label: {
                color: 'white',
                show: true,
                position: 'top'
              }
            }
          }
        },
        {
          name: '累计死亡病例',
          type: 'line',
          data: dataDeath,
          //[0, 1, 2, 3, 5, 21, 25, 150, 215, 220, 221],
          lineStyle: {
            normal: {
              color: 'gray',
              opacity: 1
            }
          },
          itemStyle: {
            normal: {
              label: {
                color: 'white',
                show: true,
                position: 'top'
              }
            }
          }
        },
        {
          name: '累计密接人数',
          type: 'line',
          data: dataCloseContact,
          //[0, 50, 210, 340, 550, 4060, 20700, 34000, 35500, 42500, 46800],
          lineStyle: {
            normal: {
              color: 'yellow',
              opacity: 1
            }
          },
          itemStyle: {
            normal: {
              label: {
                color: 'white',
                show: true,
                position: 'top'
              }
            }
          }
        }
      ]
    };
    this.chart6.setOption(option);
  },

  //疫情风险预警
  chartFunc7(){
    // 3天后确认人数预测
    let diagNumFuture = this.unLinearRegression(diagData, 9);
    // 近7日传播率
    let contSum = 0;
    let diagSum = 0;
    for(let i=0; i< diagData.length; i++){
      diagSum += diagData[i];
      contSum += contactData[i];
    }
    let contDiagRate = contSum/diagSum;
    // 近7日平均外部输入
    let inputDiagSum = 0;
    for(let i=0; i<inputDiagnostic.length; i++){
      inputDiagSum += inputDiagnostic[i];
    }
    let inputDiagAvg = inputDiagSum/inputDiagnostic.length;

    //当前各项疫情风险评分
    //未来感染人数
    let scoreDiagNumFuture = 4;
    if(diagNumFuture>50){
      scoreDiagNumFuture = 7;
    } else if(diagNumFuture>150){
      scoreDiagNumFuture = 10;
    }
    //传播率评分
    let scoreContDiagRate = 4;
    if(contDiagRate>5){
      scoreContDiagRate = 7;
    } else if(contDiagRate>10){
      scoreContDiagRate = 10;
    }
    //暴露性评分
    let scoreInputDiagAvg = 4;
    if(inputDiagAvg>5){
      scoreInputDiagAvg = 7;
    }else if (inputDiagAvg>10){
      scoreInputDiagAvg = 10;
    }
    //抗灾能力评分
    let scoreMedicalLoad = 4;
    if(medicalLoad>0.85){
      scoreMedicalLoad = 7;
    } else if(medicalLoad>0.95){
      scoreMedicalLoad = 10;
    }
    //疫情风险总评分
    let score = scoreDiagNumFuture*0.4 + scoreContDiagRate*0.2 + scoreInputDiagAvg*0.15 + scoreMedicalLoad*0.25;
    //console.log(score);

    //疫情预警信息
    let warningInform = '';
    if(score<5.5){
      this.renderChart7(3);
      warningInform = warningInform+'疫情风险低';
    } else if(5.5<score<8.5){
      this.renderChart7(2);
      warningInform = warningInform+',疫情扩散阶';
    } else if(score>8.5){
      this.renderChart7(1);
      warningInform = warningInform+',疫情紧急';
    }
    if(scoreContDiagRate===4){
      warningInform = warningInform+',动态清零';
    }else if(scoreContDiagRate===7){
      warningInform = warningInform+',流动管控';
    } else if(scoreContDiagRate===10){
      warningInform = warningInform+',区域封控';
    }
    if(scoreInputDiagAvg===4){
      warningInform = warningInform+',防输入';
    }else if(scoreInputDiagAvg===7){
      warningInform = warningInform+',枢纽管控';
    }else if(scoreInputDiagAvg===10){
      warningInform = warningInform+',暂停跨域流动';
    }
    if(scoreInputDiagAvg===7){
      warningInform = warningInform+',协调医疗资源';
    }else if(scoreInputDiagAvg===10){
      warningInform = warningInform+',跨域医疗支持';
    }
    return warningInform;
  },

  renderChart7( level ) {
    this.chart7_1 = echarts.init(document.getElementById('chart7-alert-1'));
    let option1 = this.setChart7Option('red', '#420E0A');
    this.chart7_1.setOption(option1);

    this.chart7_2 = echarts.init(document.getElementById('chart7-alert-2'));
    let option2 = this.setChart7Option('yellow', '#4F4E1A');
    this.chart7_2.setOption(option2);

    this.chart7_3 = echarts.init(document.getElementById('chart7-alert-3'));
    let option3 = this.setChart7Option('green', '#075C07');
    this.chart7_3.setOption(option3);

    switch (level){
      case 1:
        this.chart7_1 = echarts.init(document.getElementById('chart7-alert-1'));
        let option1 = this.setChart7Option('red', '#ED3A1D');
        this.chart7_1.setOption(option1);
        break;
      case 2:
        this.chart7_2 = echarts.init(document.getElementById('chart7-alert-2'));
        let option2 = this.setChart7Option('yellow', '#FFF200');
        this.chart7_2.setOption(option2);
        break;
      case 3:
        this.chart7_3 = echarts.init(document.getElementById('chart7-alert-3'));
        let option3 = this.setChart7Option('green', '#75F94D');
        this.chart7_3.setOption(option3);
        break;
    }

  },

  setChart7Option(color1, color2) {
    let option = {
      series: [
        {
          name: "外圈",
          type: "pie",
          hoverAnimation: false,
          clockwise: false,
          radius: ["70%", "70%"],
          center: ["50%", "50%"],
          data: [10],
          itemStyle: {
            normal: {
              borderWidth: 1.5,
              borderType: "dotted",//dotted 虚线
              borderColor: color1,// 虚线颜色
              opacity: 0.5,
            },
          },
          label: {
            normal: {
              show: false,
            },
            emphasis: {
              show: false,
              textStyle: {
                fontSize: "14",
              },
            },
          },
          labelLine: {
            normal: {
              show: false,
            },
          },
        },
        {
          name: "内圈",
          type: "pie",
          hoverAnimation: false,
          clockwise: false,
          radius: ["65%", "65%"],
          center: ["50%", "50%"],
          data: [10],
          itemStyle: {
            normal: {
              borderWidth: 1.5,
              borderType: "dotted",//dotted 虚线
              borderColor: color1,// 虚线颜色
              opacity: 0.5,
            },
          },
          label: {
            normal: {
              show: false,
            },
            emphasis: {
              show: false,
              textStyle: {
                fontSize: "14",
              },
            },
          },
          labelLine: {
            normal: {
              show: false,
            },
          },
        },
        {
          name: "实心圆",
          type: "pie",
          hoverAnimation: false,
          clockwise: false,
          radius: "55%",
          center: ["50%", "50%"],
          data: [10],
          itemStyle: {
            normal: {
              color: color2,
            },
          },
          label: {
            normal: {
              show: false,
            },
            emphasis: {
              show: false,
              textStyle: {
                fontSize: "14",
              },
            },
          },
          labelLine: {
            normal: {
              show: false,
            },
          },
        }
      ]
    };
    return option;
  },

  //医疗资源占用率-仪表
  chartFunc8() {
    //获取数据并计算核酸采样率
    request({
      url: '/eim/check/getCurrentNum',
      method: 'post'
    }).then(res1 => {
        request({
          url: '/eim/contact_info/getCurrentNum',
          method: 'post'
        }).then(res2 => {
          let checkRate = (res1.data/res2.data*100).toFixed(2);
          this.chart8_1 = echarts.init(document.getElementById('chart8-pie-1'));
          let option1 = this.setChart8Option(checkRate, '#027825', '核酸采样率');
          this.chart8_1.setOption(option1);
        });
    });
    //获取数据并计算医疗资源占用率
    request({
      url: '/eim/log/selectAllCurrentEimTaskLog',
      method: 'post'
    }).then(res1=> {
      request({
        url: '/eim/person/getAllMedicalPersonnel',
        method: 'post'
      }).then(res2=> {

        let workingTime = 0;
        for (let i = 0; i < res1.data.length; i++) {
          if (res1.data[i].taskStatus === '3') {
            let flag1 = true;
            for (let j = 0; j < res1.data.length; j++) {
              if (res1.data[i].taskId === res1.data[j].taskId && res1.data[j].taskStatus === '2') {
                workingTime += (new Date(res1.data[i].createTime) - new Date(res1.data[j].createTime)) / 60 / 60 / 1000;
                flag1 = false;
                break;
              }
            }
            if (flag1) {
              let date = new Date(res1.data[i].createTime);
              workingTime += date.getHours() + date.getMinutes() / 60;
            }
          }
          if (res1.data[i].taskStatus === '2') {
            let flag2 = true;
            for (let j = 0; j < res1.data.length; j++) {
              if (res1.data[i].taskId === res1.data[j].taskId && res1.data[j].taskStatus === '3') {
                flag2 = false;
                break;
              }
            }
            if (flag2) {
              let date = new Date(res1.data[i].createTime);
              workingTime += (23 - date.getHours()) + (60 - date.getMinutes()) / 60;
            }
          }
        }

        let loadRate = ((workingTime/(res2.data*40))*100).toFixed(2);
        // 定义全局变量
        medicalLoad = loadRate;

        this.chart8_2 = echarts.init(document.getElementById('chart8-pie-2'))
        let option2 = this.setChart8Option(loadRate, '#006DD6', '医疗资源占用率');
        this.chart8_2.setOption(option2);
      });
    });
  },

  setChart8Option(parm, color, name){
    let option = {
      series: [
        {
          type: 'gauge',
          name: name,
          // 显示数值
          data: [ { value: parm, name: name}],
          // 标题
          title: {
            offsetCenter: [0, '110%'],
            color: "#fff",  //字体颜色
            fontSize: 16,
          },
          // 仪表中心位置
          center: ['50%', '50%'],
          // 数据显示格式
          detail: {
            formatter: '{value}%',
            offsetCenter: [0, 0],
            color: "#FFF",
            fontSize: 18
          },
          // 半径大小
          radius: '80%',  //环形的大小
          clockwise: true,
          roundCap: true,

          // 仪表图形设置
          axisLine: {
            show: true,
            lineStyle: {
              color: [
                [parm/100, color],
                [1, '#FEFFFE11']  //基底大圈比例为1，
              ],
              width: 15,  //环形的粗细
            }
          },
          splitLine: {
            show: false   //分隔线
          },
          axisTick: {
            show: false  //仪表盘刻度
          },
          axisLabel: {
            show: false
          },
          pointer: {
            show: false  //不显示仪表盘指针
          },
        }
      ]
    }
    return option;
  },

  //获取长春各区数据
  getAddressById( id ){
    switch (id){
      case "220102":
        return "南关区";
      case "220103":
        return "宽城区";
      case "220104":
        return "朝阳区";
      case "220105":
        return "二道区";
      case "220106":
        return "绿园区";
      case "220112":
        return "双阳区";
      case "220113":
        return "九台区";
      case "220122":
        return "农安县";
    }
  },

  //获取近七日日期数据
  getNearly7Day() {
    let days = []
    let date = new Date()
    for (let i = 0; i <= 24 * 6; i += 24) {
      //今天加上前6天
      let dateItem = new Date(date.getTime() - i * 60 * 60 * 1000) //使用当天时间戳减去以前的时间毫秒（小时*分*秒*毫秒）
      let y = dateItem.getFullYear() //获取年份
      let m = dateItem.getMonth() + 1 //获取月份js月份从0开始，需要+1
      let d = dateItem.getDate() //获取日期
      m = addDate0(m) //给为单数的月份补零
      d = addDate0(d) //给为单数的日期补零
      let valueItem = y + '-' + m + '-' + d //组合
      days.push(valueItem) //添加至数组
    }
    //给日期加0
    function addDate0(time) {
      if (time.toString().length == 1) {
        time = '0' + time.toString()
      }
      return time
    }
    return days
  },

  //二次回归
  unLinearRegression(data, x){
    let alpha0=0;
    let alpha1=0;
    let beta0=0;
    let a0=0;
    let a1=0;
    let a2=0;

    let xsum=0; //x的和
    let ysum=0; //y的和

    for(let i=0;i<data.length;i++){
      xsum+=data[i].x;
      ysum+=data[i].y;
    }
    let length=data.length;//p0(x)=1,所以p0(x)的和就是数据长度
    alpha0 =xsum/data.length;//α0
    let p1data=[];//p1(x)
    let p1p1sum=0;//p1*p1 多项式的和
    let xp1p1sum=0;//x*p1*p1 多项式的和
    for(let i=0;i<data.length;i++){
      let p1=data[i].x-alpha0;
      p1p1sum+=p1*p1;
      xp1p1sum+=data[i].x*p1*p1;
      p1data.push(p1);
    }
    alpha1=xp1p1sum/p1p1sum;//α1
    beta0=p1p1sum/length;//β0
    let p2data=[];//p2(x)
    let p2p2sum=0;//p2*p2 多项式的和
    let xp2p2sum=0;//x*p2*p2 多项式的和
    for(let i=0;i<data.length;i++){
      let p2=(data[i].x-alpha1)*p1data[i]-beta0;
      p2data.push(p2);
      p2p2sum+=p2*p2;
      xp2p2sum+=data[i].x*p2*p2;
    }
    //var alpha2=xp2p2sum/p2p2sum;
    let fp0=ysum;//y*p0 多项式的和
    let fp1=0;//y*p1 多项式的和
    let fp2=0;//y*p2 多项式的和
    for(let i=0;i<data.length;i++){
      fp1+=data[i].y*p1data[i];
      fp2+=data[i].y*p2data[i];
    }
    a0=fp0/length;//g(x)=a0*p0(x)+a1*p1(x)+a2*p2(x) 二次多项式拟合公式
    a1=fp1/p1p1sum;
    a2=fp2/p2p2sum;

    let pp1x=x-alpha0;
    let pp2x=(x-alpha1)*(x-alpha0)-beta0;
    let y=(a0+a1*pp1x+a2*pp2x);
    return y;
  },

}
